/*eslint-disable*/
import Vue from 'vue'

//导入VXETable
import 'xe-utils'
import VXETable from 'vxe-table'
import '@/styles/vxe-table-3.0.0-style.css'
import VXETablePluginElement from 'vxe-table-plugin-element'
import 'vxe-table-plugin-element/dist/style.css'

import MatrDownTable from '@/components/vxe/MatrDownTable.vue'
import MatrUnitDownTable from '@/components/vxe/MatrUnitDownTable.vue'
import FortenGrid from '@/components/common/FortenGrid.vue'
import FortenDataviewDown from '@/components/common/FortenDataviewDown.vue'
import FortenDataviewDownSelect from '@/components/common/FortenDataviewDownSelect.vue'
import DatePicker from '@/components/vxe/DatePicker.vue'
import FortenSelect from '@/components/common/FortenSelect.vue'
import FortenVxeDate from '@/components/common/FortenVxeDate.vue'


Vue.component(MatrDownTable.name, MatrDownTable)
Vue.component(MatrUnitDownTable.name, MatrUnitDownTable)
Vue.component(FortenGrid.name, FortenGrid)
Vue.component(DatePicker.name, DatePicker)
Vue.component(FortenSelect.name, FortenSelect)
Vue.component(FortenVxeDate.name, FortenVxeDate)
Vue.component(FortenDataviewDownSelect.name, FortenDataviewDownSelect)
// VXETable.interceptor.add('event.clearActived', (params, event) => {
//   // 比如点击了某个组件的弹出层面板之后，此时被激活单元格不应该被自动关闭，通过返回 false 可以阻止默认的行为。
//   return false
// })

// 创建Matr渲染
VXETable.renderer.add('MatrDownTable', {
  autofocus: '.vxe-input--inner',
  renderEdit(h, renderOpts, params) {
    return [
      <matr-down-table params={ params } renderOpts={ renderOpts }></matr-down-table>
    ]
  }
})

VXETable.renderer.add('FortenDataviewDown', {
  autofocus: '.vxe-input--inner',
  renderEdit(h, renderOpts, params) {
    return [
      <FortenDataviewDown params={ params } renderOpts={ renderOpts }></FortenDataviewDown>
  ]
  }
})
VXETable.renderer.add('ElSelect', {
  autofocus: '.vxe-input--inner',
  renderEdit(h, renderOpts, params) {
    return [
      <ElSelect params={ params } renderOpts={ renderOpts }></ElSelect>
  ]
  }
})

VXETable.renderer.add('DatePicker', {
  renderEdit(h, renderOpts, params) {
    return [
      <date-picker params={ params } renderOpts={ renderOpts } ></date-picker>
    ]
  }
})

//创建MatrUnit渲染
VXETable.renderer.add('MatrUnitDownTable', {
  renderEdit(h, renderOpts, params) {
    return [
      <matr-unit-down-table params={ params } renderOpts={ renderOpts }></matr-unit-down-table>
    ]
  }
})

//通用下拉表格
VXETable.renderer.add('FortenGrid', {
  renderEdit(h, renderOpts, params) {
    return [
      <FortenGrid params={ params } renderOpts={ renderOpts }></FortenGrid>
    ]
  }
})


//通用下拉dataview
VXETable.renderer.add('FortenDataviewDown', {
  renderEdit(h, renderOpts, params) {
    return [
      <FortenDataviewDown params={ params } renderOpts={ renderOpts }></FortenDataviewDown>]
  }
})

//通用下拉dataview，嵌套在数据视图表单区域
VXETable.renderer.add('FortenDataviewDownSelect', {
  renderItemContent(h, renderOpts, params) {
    return [
      <FortenDataviewDownSelect params={ params } renderOpts={ renderOpts }></FortenDataviewDownSelect>]
  }
})

//通用选择框
VXETable.renderer.add('FortenSelect', {
  renderItemContent(h, renderOpts, params) {
    return [
      <FortenSelect params={ params } renderOpts={ renderOpts }></FortenSelect>]
  }
})

//通用下拉dataview
VXETable.renderer.add('FortenVxeDate', {
  renderEdit(h, renderOpts, params) {
    return [
      <FortenVxeDate params={ params } renderOpts={ renderOpts }></FortenVxeDate>]
  }
})

/**
 * @Description：清除激活单元格之前触发,允许返回 false 阻止默认行为
 * 比如点击了某个组件的弹出层面板之后，此时被激活单元格不应该被自动关闭，通过返回 false 可以阻止默认的行为
 */
VXETable.interceptor.add('event.clearActived', (params, event) => {
  console.log('VXETable.interceptor.clearActived',params,event)
  /**
   * @Description：${description}
   * 当可编辑单元格，弹出 FortenDataviewDown，并且FortenDataviewDown中有下拉框查询选项时，
   * 针对vxe-pulldown--panel、el-popper取消默认关闭行为，
   * 可实现选择完下拉框，单元格保持激活，并且FortenDataviewDown面板不关闭
   * 比如：科室请领，明细选择耗材面板时，筛选耗材类型下拉框
   *    el-popper 为 elementui 下拉框面板的外部容器
   *  vxe-pulldown--panelr 为 vxe-pulldown的外部容器
   */
  let filter = ['.vxe-pulldown--panel', '.el-popper', '.vxe-table-clear-actived', '.custom-select']

  // 日期面板
  filter.push('.t-date-picker__panel-container')
  let target = $(event.target)
  for(let hitIndex in filter) {
    let hit = filter[hitIndex]
    if (target && target.closest(hit).length > 0) {
      throw Error('此异常可以忽略，为了阻止清除单元格操作')
      return false
    }
  }
  return true
})

VXETable.use(VXETablePluginElement)
Vue.use(VXETable)
